package navigation;

/**
 *
 * @author hyurah
 */

public class GoRightExitNavigator extends Navigator{

	public GoRightExitNavigator() {
		super();
	}

	public GoRightExitNavigator(Navigator oldNavigator) {
		super(oldNavigator);
	}

	public int decideDirection() {
		int direction;
		int xTarget;
		int yTarget;
		if(map.fieldIsExit(xPosition, yPosition)) {
			direction=-1;
		} else {
			direction=Direction.RIGHT;
			boolean found=false;
			for(int i=0; i<4 && !found; i++) {
				if(isPassable((direction+facingDirection)%4)) {
					xTarget = xPosition;
					yTarget = yPosition;
					switch((direction+facingDirection)%4) {
						case Direction.NORTH: yTarget--; break;
						case Direction.EAST: xTarget++; break;
						case Direction.SOUTH: yTarget++; break;
						case Direction.WEST: xTarget--; break;
					}
					if(map.fieldIsExit(xTarget, yTarget)) {
						found=true;
					} else if(!map.fieldIsKnown(xTarget, yTarget)) {
						found=true;
					}
				}
				if(!found) {
					direction=(direction+Direction.LEFT)%4;
				}
			}
			for(int i=0; i<3 && !found; i++) {
				if(isPassable((direction+facingDirection)%4)) {
					xTarget = xPosition;
					yTarget = yPosition;
					switch((direction+facingDirection)%4) {
						case Direction.NORTH: yTarget--; break;
						case Direction.EAST: xTarget++; break;
						case Direction.SOUTH: yTarget++; break;
						case Direction.WEST: xTarget--; break;
					}
					if(!map.fieldIsDeadEnd(xTarget, yTarget)) {
						found=true;
					}
				}
				if(!found) {
					direction=(direction+Direction.LEFT)%4;
				}
			}
		}
		return direction;
	}

}
